package made.ann;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import made.util.Config;
import made.util.Logger;
import made.util.Config.DEBUG_LEVEL;

public class Data implements Config
{
    final List<List<Float>> _inputs;
    final List<List<Float>> _outputs;
    final int _maxIterations;
    final float _learningRate;
    final float _momentum;

    public Data(String fileName, float learningRate, float momentum, int maxIterations)
    {
        _inputs = new ArrayList<List<Float>>();
        _outputs = new ArrayList<List<Float>>();
        _learningRate = learningRate;
        _momentum = momentum;
        _maxIterations = maxIterations;
        init(fileName);
    }

    public Data(String fileName)
    {
        _inputs = new ArrayList<List<Float>>();
        _outputs = new ArrayList<List<Float>>();
        _learningRate = -1.0f;
        _momentum = -1.0f;
        _maxIterations = -1;
        init(fileName);
    }

    public List<List<Float>> getInputs()
    {
        return _inputs;
    }

    public List<List<Float>> getOutputs()
    {
        return _outputs;
    }

    public float getLearningRate()
    {
        return _learningRate;
    }

    public float getMomentum()
    {
        return _momentum;
    }

    public int getMaxIterations()
    {
        return _maxIterations;
    }

    protected final void init(String fileName)
    {
        Logger.logLine("Initializing new training data");
        File file = new File(fileName);
        String nextLine = "";
        Scanner fileScanner;

        try
        {
            fileScanner = new Scanner(new FileReader(file));

            try
            {
                while (fileScanner.hasNext())
                {
                    nextLine = fileScanner.nextLine();
                    Scanner lineScannerInner = new Scanner(nextLine);
                    lineScannerInner.useDelimiter(" ");

                    float edibleTime = Float.parseFloat(lineScannerInner.next());
                    float nearestPillDistance = Float.parseFloat(lineScannerInner.next());
                    float nearestGhostDistance = Float.parseFloat(lineScannerInner.next());
                    float toNearestPillAction = Float.parseFloat(lineScannerInner.next());
                    float toNearestGhostAction = Float.parseFloat(lineScannerInner.next());
                    float fromNearestGhostAction = Float.parseFloat(lineScannerInner.next());

                    ArrayList<Float> nextInput = new ArrayList<Float>(3);
                    ArrayList<Float> nextOutput = new ArrayList<Float>(3);
                    nextInput.add(edibleTime);
                    nextInput.add(nearestPillDistance);
                    nextInput.add(nearestGhostDistance);
                    nextOutput.add(toNearestPillAction);
                    nextOutput.add(toNearestGhostAction);
                    nextOutput.add(fromNearestGhostAction);

                    _inputs.add(nextInput);
                    _outputs.add(nextOutput);

                    lineScannerInner.close();
                }
            } catch (Exception e)
            {
                Logger.log("An error ocurred while reading the file '" + fileName + "'");
            } finally
            {
                fileScanner.close();
            }
        } catch (FileNotFoundException e)
        {
            Logger.log("Could not find the file '" + fileName + "'");
        }

        int numDataSets = _inputs.size();
        int numInputs = _inputs.get(0).size();
        int numOutputs = _outputs.get(0).size();
        
        if (MPM_DEBUG_LEVEL == DEBUG_LEVEL.VERBOSE)
        {
            for (int i = 0; i < numDataSets; i++)
            {

                Logger.logMethod();
                Logger.log("Input: ");

                for (int j = 0; j < numInputs; j++)
                {
                    System.out.print(_inputs.get(i).get(j) + " ");
                }

                Logger.log("Output: ");

                for (int j = 0; j < numOutputs; j++)
                {
                    Logger.log(_outputs.get(i).get(j) + " ");
                }

                Logger.logLine();
            }
        }
    }
}